package com.diandian.backend.douyin.life.exchange.service;

import com.alibaba.fastjson.JSON;
import com.diandian.backend.douyin.life.exchange.req.MemberMobileChangeReq;
import com.diandian.backend.douyin.life.exchange.req.MemberPointsChangeReq;
import com.diandian.backend.douyin.life.exchange.req.MemberUnbindChangeReq;
import com.diandian.douyin.life.boot.starter.dto.request.MemberInfoChangeReq;
import com.diandian.douyin.life.boot.starter.dto.request.MemberInfoChangeReqMember;
import com.diandian.douyin.life.boot.starter.dto.response.DouyinLifeMemberInfoChangeResponse;
import com.diandian.douyin.life.boot.starter.service.member.DouyinLifeMemberBasicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class MemberInfoChangeService {

    @Autowired
    private DouyinLifeMemberBasicService douyinLifeMemberBasicService;

    private static final Logger logger = LoggerFactory.getLogger(MemberInfoChangeService.class);

    /**
     * 积分等级更新
     * @param account
     * @param data
     * @return
     */
    public DouyinLifeMemberInfoChangeResponse pointsChange(String token, String account, MemberPointsChangeReq data) {

        logger.info("接收到会员积分变更数据：{}", JSON.toJSONString(data));

        // 参数校验
        data.check();

        // 组装请求对象
        MemberInfoChangeReq req = new MemberInfoChangeReq();
        req.setAccount_id(account);
        MemberInfoChangeReqMember item = new MemberInfoChangeReqMember();
        item.setOpen_id(data.getOpenId());
        item.setPoints_amount_cent(data.getPointsAmountCent());
        if(data.getPointsUpdateCent() != null){
            item.setPoints_update_cent(data.getPointsUpdateCent());
        }
        item.setUser_level(data.getUserLevel());
        item.setUpdate_time(data.getUpdateTime());
        req.setMembers(Arrays.asList(item));

        // 执行请求
        return douyinLifeMemberBasicService.memberInfoUpdate(token, req);

    }

    /**
     * 手机号更新
     * @param account
     * @param data
     * @return
     */
    public DouyinLifeMemberInfoChangeResponse mobileChange(String token, String account, MemberMobileChangeReq data) {

        logger.info("接收到会员手机号变更数据：{}", JSON.toJSONString(data));

        // 参数校验
        data.check();

        // 组装请求对象
        MemberInfoChangeReq req = new MemberInfoChangeReq();
        req.setAccount_id(account);
        MemberInfoChangeReqMember item = new MemberInfoChangeReqMember();
        item.setOpen_id(data.getOpenId());
        item.setNew_mobile(data.getNewMobile());
        item.setPoints_amount_cent(0); // 默认赋值，抖音不会处理
        item.setUser_level(1); // 默认赋值，抖音不会处理
        item.setUpdate_time(data.getUpdateTime());
        req.setMembers(Arrays.asList(item));

        // 执行请求
        return douyinLifeMemberBasicService.memberInfoUpdate(token, req);
    }

    /**
     * 会员解绑
     * @param account
     * @param data
     * @return
     */
    public DouyinLifeMemberInfoChangeResponse unbindChange(String token, String account, MemberUnbindChangeReq data) {

        logger.info("接收到会员解绑变更数据：{}", JSON.toJSONString(data));

        // 参数校验
        data.check();

        // 组装请求对象
        MemberInfoChangeReq req = new MemberInfoChangeReq();
        req.setAccount_id(account);
        MemberInfoChangeReqMember item = new MemberInfoChangeReqMember();
        item.setOpen_id(data.getOpenId());
        item.setUnbind(true);
        item.setPoints_amount_cent(0); // 默认赋值，抖音不会处理
        item.setUser_level(1); // 默认赋值，抖音不会处理
        item.setUpdate_time(data.getUpdateTime());
        req.setMembers(Arrays.asList(item));

        // 执行请求
        return douyinLifeMemberBasicService.memberInfoUpdate(token, req);
    }

}
